package test09;

import java.io.*;
import java.util.Scanner;

public class Test09 {

    /**
     *  通过控制台输入一个字符串并将该字符串以字节为单位存储到文件中，将文件保存后的数据再读入到程序中进行显示
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        System.out.println("=============第九题=====================");
        test09();
    }

    public  static void  test09() throws IOException {
        // 创建文件及文件流
        File file1 = new File("a.txt");
        FileOutputStream fs = new FileOutputStream(file1, true);

        // 接收输入
        Scanner scanner = new Scanner(System.in);
        System.out.print("Input content you want to save: ");
        String str = scanner.nextLine();

        // 字符串转字节数组写入
        byte[] bytesStr = str.getBytes();
        fs.write(bytesStr);

        // 读取字节数据
        FileInputStream fis = new FileInputStream("a.txt");
        int b;
        while ((b = fis.read()) != -1) {
            System.out.print((char) b);
        }

        scanner.close();
        fs.close();
        fis.close();

    }
    }

